<template>
  <transition name="slide">
    <music-list :title="title" :bgImage="bgImage" :songs="songs"></music-list>
  </transition>
</template>

<script>
import { mapMutations, mapState } from 'vuex'
import MusicList from '@/components/common/MusicList'
export default {
  components: {
    MusicList
  },
  data () {
    return {
      songs: []
    }
  },
  methods: {
    // 判断VUEX是否存在songList 不存在时返回  -----即 刷新时返回前一页面功能
    ...mapMutations(['setSongList']),
    checksongList () {
      if (!this.songList.content_id) {
        this.$router.back()
      }
    },
    // 获取当前歌单/专辑的热门歌曲
    async getsongListDetail () {
      // 清空songs 这样重新申请数据就可以出现加载效果
      this.songs = []
      const {
        data: { data: res }
      } = await this.$http.get('/api/songlist?id=' + this.songList.content_id)
      this.songs = res.songlist
      // 搜索结果为空
      if (this.songs === undefined) {
        this.setSongList([])
        return
      }
      this.songs.forEach(item => {
        item.mid = item.songmid
        item.name = item.songname
        item.album = {
          id: item.albumid,
          mid: item.albummid,
          name: item.albumname
        }
      })
    }
  },
  computed: {
    ...mapState(['songList']),
    // 返回当前点击的歌单/专辑的标题
    title () {
      return this.songList.title
    },
    // 返回当前点击的歌单/专辑的封面
    bgImage () {
      return this.songList.cover
    }
  },
  mounted () {
    // 加载判断是否返回
    this.checksongList()
    // 加载当前歌单详情歌曲
    this.getsongListDetail()
  },
  watch: {
    songList () {
      this.getsongListDetail()
    }
  }
}
</script>

<style lang="stylus" scoped>
.slide-enter, .slide-leave-to {
  transform: translateX(100%);
}
.slide-enter-active, .slide-leave-active {
  transition: all .3s;
}
</style>
